from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import baozou_insert_sql
import time
import re
import excel_spider_sql
list1  = excel_spider_sql.getConditions()
username = [""]
def baozou_novel():
    # for x in range(0,3):
    for x in range(len(list1)):
        if list1[x]["Platform"] == "暴走1":
            username = ""
        elif list1[x]["Platform"] == "暴走2":
            username = ""
        elif list1[x]["Platform"] == "暴走3":
            username = ""
        else:
            print("无符合条件的平台")
            continue
            # break
        driver = webdriver.Chrome(r'D:\Program Files\python3.63\chromedriver.exe')
        #进入暴走小说平台
        driver.get("https://cps.baozoukanshu.com/admin/login.html")
        driver.maximize_window()
        driver.implicitly_wait(15)
        #点击登录渠道
        try:
            WebDriverWait(driver,15).until(
                EC.element_to_be_clickable((By.XPATH,'//div/div[1]//div[3]/a'))
            )
            driver.find_element_by_xpath('//div/div[1]//div[3]/a').click()
        except Exception:
            print("点击登录渠道出错")
        #清理账号
        time.sleep(1)
        driver.find_element_by_xpath('//*[@id="account"]').clear()
        driver.find_element_by_xpath('//*[@id="account"]').send_keys(username)
        #清理密码
        driver.find_element_by_xpath('//*[@id="password"]').clear()
        driver.find_element_by_xpath('//*[@id="password"]').send_keys('')
        time.sleep(5)
        driver.find_element_by_xpath('//*[@id="submit_btn"]').click()
        #点击已生成链接管理
        try:
            WebDriverWait(driver,15).until(
                EC.element_to_be_clickable((By.XPATH,'//li[4]//cite'))
            )
            driver.find_element_by_xpath('//li[4]//cite').click()
        except Exception:
            print("点击已生成链接管理出错")


        # 点击选择时间
        num = driver.find_element_by_xpath('//li[12]/a').text
        num = int(num)
        for y in range(0,5):
            body = driver.page_source
            html = body.replace('\n', '').replace('\r', '').replace('\t', '')
            # 渠道名称
            Public_platform_list = re.findall('data-cost="0">(.*?)</b>', html)

            # 推广链接url
            url_list = re.findall('https://m.(.*?)</p>', html)

            # 创建时间
            Creation_time_list = re.findall('class="p">创建时间：(.*?)</p>', html)

            # 推荐的小说名
            Novel_name_list = re.findall('<p class="p">书名/页面名：(.*?)</p>', html)

            # 原文点击次数
            Number_clicks_list = re.findall('创建时间：.*?</p>.*?<td><span class="c-red">(.*?)</span>', html)

            # 充值金额
            Recharge_amount_list = re.findall('<td><span class="c-red">￥(.*?)</span></td>',html)

            for i in range(len(Public_platform_list)):
                Public_platform = Public_platform_list[i]
                url = url_list[i]
                url = "https://m."+ url
                Creation_time = Creation_time_list[i]
                Novel_name = Novel_name_list[i]
                Number_clicks = Number_clicks_list[i]
                Recharge_amount = Recharge_amount_list[i]
                # 判断数据库中是否已存在此推广链接
                if list1[x]["date_time"] in Public_platform:
                    if list1[x]["Platform"] == "暴走3":
                        a = baozou_insert_sql.xiuse_select_novel_mysql(url="https://m."+url_list[i])
                        if a == 1:
                            # 存在 替换原有数据

                            baozou_insert_sql.xiuse_Update_novel_mysql(Public_platform,url,Creation_time,Novel_name,Number_clicks,Recharge_amount)
                            print("羞涩书吧 更新数据")
                        else:
                            # 否则  插入数据
                            baozou_insert_sql.xiuse_insert_novel_mysql(Public_platform,url,Creation_time,Novel_name,Number_clicks,Recharge_amount)
                            print("羞涩书吧 插入数据")
                    elif list1[x]["Platform"] == "暴走2":
                        a = baozou_insert_sql.moqing_select_novel_mysql(url="https://m."+url_list[i])
                        if a == 1:
                            # 存在 替换原有数据
                            baozou_insert_sql.moqing_Update_novel_mysql(Public_platform,url,Creation_time,Novel_name,Number_clicks,Recharge_amount)
                            print("魔情书斋 更新数据")
                        else:
                            # 否则  插入数据
                            baozou_insert_sql.moqing_insert_novel_mysql(Public_platform,url,Creation_time,Novel_name,Number_clicks,Recharge_amount)
                            print("魔情书斋 插入数据")
                    else:
                        a = baozou_insert_sql.meise_select_novel_mysql(url="https://m."+url_list[i])
                        if a == 1:
                            # 存在 替换原有数据
                            baozou_insert_sql.meise_Update_novel_mysql(Public_platform, url, Creation_time, Novel_name,Number_clicks,Recharge_amount)
                            print("媚色小说 更新数据")
                        else:
                            # 否则  插入数据
                            baozou_insert_sql.meise_insert_novel_mysql(Public_platform, url, Creation_time, Novel_name,Number_clicks,Recharge_amount)
                            print("媚色小说 插入数据")
                else:
                    pass

            # 点击下一页
            if y == num - 1:
                pass
            else:
                time.sleep(1)
                driver.find_element_by_xpath("//a[text()='下一页']").click()
        #关闭浏览器
        driver.quit()
    #休眠
    print("休眠10分钟")
    time.sleep(600)
if __name__ == "__main__":
    baozou_novel()
